package com.mall.b2c.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.mall.b2c.dao.TagDao;
import com.mall.b2c.domain.goods.TagDomain;
import com.mall.b2c.service.TagService;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;
import com.mall.common.util.Collections3;

@Service
@Transactional(readOnly=true)
public class TagServiceImpl extends BaseServiceImpl<TagDomain, Long> implements
		TagService {
	
	@Resource
	public void setBaseDao(TagDao tagDao) {
		super.setBaseDao(tagDao);
	}

	@Override
	public List<TagDomain> getTagsByGoods(Long[] goods_ids) {
		String hql = "select distinct bean from TagDomain bean join bean.goodses goods where goods.id in (:goods_ids)";
		Finder f = Finder.create(hql);
		f.setParamList("goods_ids", goods_ids);
		return f.findHQLList();
	}
	
	@Override
	public TagDomain findByNameForTag(String tagName){
		Assert.notNull(tagName, "标签名称不能为空!");
		String hql = "from TagDomain bean where bean.name=:tagName";
		Finder f = Finder.create(hql);
		f.setParam("tagName", tagName);
		List<TagDomain> tags = f.findHQLList();
		if(Collections3.isNotEmpty(tags)){
			return tags.get(0);
		}
		return null;
	}

	@Override
	public List<TagDomain> find(Long[] ids) {
		return this.get(ids);
	}
	
}
